<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>作用域链、闭包及其它 | donyyang blog</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="1:&amp;#x4F5C;&amp;#x7528;&amp;#x57DF;&amp;#x94FE;&amp;#xFF1A;  &amp;#x5F53;&amp;#x51FD;&amp;#x6570;&amp;#x88AB;&amp;#x521B;&amp;#x5EFA;&amp;#x65F6;&amp;#xFF0C;&amp;#x5C31;&amp;#x5B58;&amp;#x5728;&amp;#x4E86;&amp;#x4F5C;&amp;#x7528;&amp;#x57DF;&amp;#x53CA;&amp;#x6267;&amp;#x884C;&amp;#x73AF;&amp;#x5">
<meta property="og:type" content="article">
<meta property="og:title" content="作用域链、闭包及其它">
<meta property="og:url" content="http://yoursite.com/2016/09/16/作用域链、闭包及其它/index.html">
<meta property="og:site_name" content="donyyang blog">
<meta property="og:description" content="1:&amp;#x4F5C;&amp;#x7528;&amp;#x57DF;&amp;#x94FE;&amp;#xFF1A;  &amp;#x5F53;&amp;#x51FD;&amp;#x6570;&amp;#x88AB;&amp;#x521B;&amp;#x5EFA;&amp;#x65F6;&amp;#xFF0C;&amp;#x5C31;&amp;#x5B58;&amp;#x5728;&amp;#x4E86;&amp;#x4F5C;&amp;#x7528;&amp;#x57DF;&amp;#x53CA;&amp;#x6267;&amp;#x884C;&amp;#x73AF;&amp;#x5">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2016-09-15T18:16:12.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="作用域链、闭包及其它">
<meta name="twitter:description" content="1:&amp;#x4F5C;&amp;#x7528;&amp;#x57DF;&amp;#x94FE;&amp;#xFF1A;  &amp;#x5F53;&amp;#x51FD;&amp;#x6570;&amp;#x88AB;&amp;#x521B;&amp;#x5EFA;&amp;#x65F6;&amp;#xFF0C;&amp;#x5C31;&amp;#x5B58;&amp;#x5728;&amp;#x4E86;&amp;#x4F5C;&amp;#x7528;&amp;#x57DF;&amp;#x53CA;&amp;#x6267;&amp;#x884C;&amp;#x73AF;&amp;#x5">
  
    <link rel="alternative" href="/atom.xml" title="donyyang blog" type="application/atom+xml">
  
  
    <link rel="icon" href="/img/body.jpeg">
  
  
      <link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.5.0/animate.min.css">
  
  
      <link href="//cdn.bootcss.com/jqueryui/1.10.4/css/jquery-ui.min.css" rel="stylesheet">
  
  <link rel="stylesheet" href="/css/style.css">
  
  <link rel="stylesheet" href="/font-awesome/css/font-awesome.min.css">
  <link rel="apple-touch-icon" href="/apple-touch-icon.png">
  
  
    <link rel="stylesheet" href="/fancybox/jquery.fancybox.css">
  
  <script src="//cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
  <script>
      var yiliaConfig = {
          fancybox: true,
          animate: true,
          isHome: false,
          isPost: true,
          isArchive: false,
          isTag: false,
          isCategory: false,
          open_in_new: false,
      }
  </script>

  
      <script>
          yiliaConfig.rootUrl = "/";
      </script>
  

  
</head>
<body>
  <div id="container">
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
    <header id="header" class="inner">
        <a href="/" class="profilepic">
            <img src="/img/body.jpeg" class="animated zoomIn">
        </a>
        <hgroup>
          <h1 class="header-author"><a href="/">donyyang</a></h1>
        </hgroup>

        
                


        
            <div id="switch-btn" class="switch-btn">
                <div class="icon">
                    <div class="icon-ctn">
                        <div class="icon-wrap icon-house" data-idx="0">
                            <div class="birdhouse"></div>
                            <div class="birdhouse_holes"></div>
                        </div>
                        <div class="icon-wrap icon-ribbon hide" data-idx="1">
                            <div class="ribbon"></div>
                        </div>
                        
                        <div class="icon-wrap icon-link hide" data-idx="2">
                            <div class="loopback_l"></div>
                            <div class="loopback_r"></div>
                        </div>
                        
                        
                        <div class="icon-wrap icon-me hide" data-idx="3">
                            <div class="user"></div>
                            <div class="shoulder"></div>
                        </div>
                        
                    </div>
                    
                </div>
                <div class="tips-box hide">
                    <div class="tips-arrow"></div>
                    <ul class="tips-inner">
                        <li>菜单</li>
                        <li>标签</li>
                        
                        <li>友情链接</li>
                        
                        
                        <li>关于我</li>
                        
                    </ul>
                </div>
            </div>
        

        <div id="switch-area" class="switch-area">
            <div class="switch-wrap">
                <section class="switch-part switch-part1">
                    <nav class="header-menu">
                        <ul>
                        
                            <li><a href="/">主页</a></li>
                        
                            <li><a href="/archives/">所有文章</a></li>
                        
                            <li><a href="/tags/">标签云</a></li>
                        
                            <li><a href="/about/">关于我</a></li>
                        
                        </ul>
                    </nav>
                    <nav class="header-nav">
                        <ul class="social">
                            
                                <li id="Email"><a class="Email" target="_blank" href="mailto:123@123.com" title="Email"></a></li>
                            
                                <li id="GitHub"><a class="GitHub" target="_blank" href="#" title="GitHub"></a></li>
                            
                                <li id="RSS"><a class="RSS" target="_blank" href="/atom.xml" title="RSS"></a></li>
                            
                        </ul>
                    </nav>
                </section>
                
                
                <section class="switch-part switch-part2">
                    <div class="widget tagcloud" id="js-tagcloud">
                        
                    </div>
                </section>
                
                
                
                <section class="switch-part switch-part3">
                    <div id="js-friends">
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="https://hexo.io">Hexo</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="https://pages.github.com/">GitHub</a>
                    
                      <a target="_blank" class="main-nav-link switch-friends-link" href="http://moxfive.xyz/">MOxFIVE</a>
                    
                    </div>
                </section>
                

                
                
                <section class="switch-part switch-part4">
                
                    <div id="js-aboutme">陌上人如玉，公子世无双</div>
                </section>
                
            </div>
        </div>
    </header>                
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
      <div class="overlay">
          <div class="slider-trigger"></div>
          <h1 class="header-author js-mobile-header hide"><a href="/" title="回到主页">donyyang</a></h1>
      </div>
    <div class="intrude-less">
        <header id="header" class="inner">
            <a href="/" class="profilepic">
                <img src="/img/body.jpeg" class="animated zoomIn">
            </a>
            <hgroup>
              <h1 class="header-author"><a href="/" title="回到主页">donyyang</a></h1>
            </hgroup>
            
            <nav class="header-menu">
                <ul>
                
                    <li><a href="/">主页</a></li>
                
                    <li><a href="/archives/">所有文章</a></li>
                
                    <li><a href="/tags/">标签云</a></li>
                
                    <li><a href="/about/">关于我</a></li>
                
                <div class="clearfix"></div>
                </ul>
            </nav>
            <nav class="header-nav">
                        <ul class="social">
                            
                                <li id="Email"><a class="Email" target="_blank" href="mailto:123@123.com" title="Email"></a></li>
                            
                                <li id="GitHub"><a class="GitHub" target="_blank" href="#" title="GitHub"></a></li>
                            
                                <li id="RSS"><a class="RSS" target="_blank" href="/atom.xml" title="RSS"></a></li>
                            
                        </ul>
            </nav>
        </header>                
    </div>
</nav>
      <div class="body-wrap"><article id="post-作用域链、闭包及其它" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/2016/09/16/作用域链、闭包及其它/" class="article-date">
      <time datetime="2016-09-15T16:40:55.000Z" itemprop="datePublished">2016-09-16</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      作用域链、闭包及其它
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
          
        <h2 id="1-&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#xFF1A;"><a href="#1-&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#xFF1A;" class="headerlink" title="1:&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#xFF1A;"></a>1:&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#xFF1A;</h2><p>  &#x5F53;&#x51FD;&#x6570;&#x88AB;&#x521B;&#x5EFA;&#x65F6;&#xFF0C;&#x5C31;&#x5B58;&#x5728;&#x4E86;&#x4F5C;&#x7528;&#x57DF;&#x53CA;&#x6267;&#x884C;&#x73AF;&#x5883;&#x3002;&#x800C;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x662F;&#x5F53;&#x51FD;&#x6570;&#x88AB;&#x6267;&#x884C;&#x65F6;&#x5B58;&#x5728;&#x7684;&#x3002;<br>  &#x5F53;&#x4EE3;&#x7801;&#x5728;&#x4E00;&#x4E2A;&#x73AF;&#x5883;&#x4E2D;&#x6267;&#x884C;&#x65F6;&#xFF0C;&#x4F1A;&#x521B;&#x5EFA;&#x53D8;&#x91CF;&#x5BF9;&#x8C61;&#x7684;&#x4E00;&#x4E2A;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x3002;&#x5176;&#x7528;&#x9014;&#x662F;&#x4FDD;&#x8BC1;&#x5BF9;&#x6267;&#x884C;&#x73AF;&#x5883;&#x6709;&#x6743;&#x8BBF;&#x95EE;&#x7684;&#x6240;&#x6709;&#x53D8;&#x91CF;&#x548C;&#x51FD;&#x6570;&#x7684;&#x6709;&#x5E8F;&#x8BBF;&#x95EE;&#xFF08;&#x9AD8;&#x7EA7;&#x6559;&#x7A0B;&#xFF09;&#x3002;&#x4F5C;&#x7528;&#x57DF;&#x94FE;&#x7684;&#x524D;&#x7AEF;&#x59CB;&#x7EC8;&#x662F;&#x5F53;&#x524D;&#x88AB;&#x6267;&#x884C;&#x7684;&#x51FD;&#x6570;&#x3002;&#x4E0B;&#x4E00;&#x4E2A;&#x662F;&#x5305;&#x542B;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x7684;&#x51FD;&#x6570;&#x6D3B;&#x7740;&#x76F4;&#x63A5;&#x65F6;window&#x5168;&#x5C40;&#x73AF;&#x5883;&#x3002;<br><a id="more"></a></p>
<p>&#x5916;&#x90E8;&#x8BBF;&#x95EE;&#x4E0D;&#x5230;&#x5185;&#x90E8;&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x3002;&#x800C;&#x5185;&#x90E8;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x5916;&#x90E8;&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x3002;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> color = <span class="string">&quot;blue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">changeColor</span>(<span class="params"></span>) </span>{</span><br><span class="line">  <span class="keyword">var</span> anotherColor = <span class="string">&quot;red&quot;</span>;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">swapColor</span>(<span class="params"></span>) </span>{</span><br><span class="line">    <span class="keyword">var</span> tempColor = anotherColor;</span><br><span class="line">    anotherColor = color;</span><br><span class="line">    color = tempColor;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//&#x53EF;&#x4EE5;&#x8BBF;&#x95EE; color,anotherColor,tempColor;</span></span><br><span class="line">  }</span><br><span class="line"></span><br><span class="line">  swapColor();  <span class="comment">//&#x53EF;&#x4EE5;&#x8BBF;&#x95EE; color,anotherColor;</span></span><br><span class="line">}</span><br><span class="line">changeColor() <span class="comment">//&#x53EA;&#x80FD;&#x8BBF;&#x95EE; color;</span></span><br></pre></td></tr></table></figure>
<p>&#x800C;&#x901A;&#x8FC7;&#x95ED;&#x5305;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x5916;&#x90E8;&#x8BBF;&#x95EE;&#x5185;&#x90E8;&#x58F0;&#x660E;&#x7684;&#x53D8;&#x91CF;&#x3002;&#x800C;&#x4E14;&#x53D8;&#x91CF;&#x4E0D;&#x88AB;&#x5783;&#x573E;&#x56DE;&#x6536;&#x673A;&#x5236;&#x56DE;&#x6536;&#x3002;</p>
<h2 id="2-&#x95ED;&#x5305;"><a href="#2-&#x95ED;&#x5305;" class="headerlink" title="2:&#x95ED;&#x5305;"></a>2:&#x95ED;&#x5305;</h2><p>&#x7B80;&#x5355;&#x6765;&#x8BB2;&#x5C31;&#x662F;&#x51FD;&#x6570;&#x4E2D;&#x5957;&#x51FD;&#x6570;</p>
<p>&#x9AD8;&#x7EA7;&#x6559;&#x7A0B;&#x5B9A;&#x4E49;&#xFF1A;&#x95ED;&#x5305;&#x662F;&#x6709;&#x6743;&#x8BBF;&#x95EE; &#x53E6;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x7684; &#x51FD;&#x6570;&#xFF1B;<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params"></span>) </span>{</span><br><span class="line">  <span class="keyword">var</span> n = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">    <span class="keyword">return</span> n++;</span><br><span class="line">  }</span><br><span class="line">}</span><br><span class="line"><span class="keyword">var</span> n = fn();</span><br><span class="line">n(); <span class="comment">//0</span></span><br><span class="line">n(); <span class="comment">//1</span></span><br><span class="line">n(); <span class="comment">//2</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">//&#x53E6;&#x4E00;&#x4E2A;</span></span><br><span class="line"><span class="keyword">var</span> color = <span class="string">&quot;blue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> obj = {</span><br><span class="line">  color:<span class="string">&quot;red&quot;</span>,</span><br><span class="line"></span><br><span class="line">  getColor:<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">    <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">this</span>.color;</span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">alert(obj.getColor()()); <span class="comment">//&#x95ED;&#x5305;</span></span><br></pre></td></tr></table></figure></p>
<h2 id="3-this&#x53CA;call-apply-bind"><a href="#3-this&#x53CA;call-apply-bind" class="headerlink" title="3:this&#x53CA;call,apply,bind"></a>3:this&#x53CA;call,apply,bind</h2><p>this&#x6307;&#x7684;&#x662F;&#x8C03;&#x7528;&#x51FD;&#x6570;&#x7684;&#x90A3;&#x4E2A;&#x5BF9;&#x8C61;&#xFF1B;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> color = <span class="string">&quot;blue&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> obj = {</span><br><span class="line">  color:<span class="string">&quot;red&quot;</span>,</span><br><span class="line"></span><br><span class="line">  getColor:{</span><br><span class="line">    color:<span class="string">&quot;purple&quot;</span>,</span><br><span class="line">    sonColor:<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">this</span>.color;</span><br><span class="line">    }</span><br><span class="line">  }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// alert(obj.getColor.sonColor()); //purple</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> n = obj.getColor.sonColor; </span><br><span class="line">alert(n()); <span class="comment">//blue;</span></span><br></pre></td></tr></table></figure>
<p>&#x7528;call,apply,bind&#x53EF;&#x4EE5;&#x6539;&#x53D8;&#x51FD;&#x6570;&#x4F53;&#x5185;this&#x7684;&#x6307;&#x5411;</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// call &#x548C; apply:&#x5728;&#x7279;&#x5B9A;&#x7684;&#x4F5C;&#x7528;&#x57DF;&#x4E2D;&#x8C03;&#x7528;&#x51FD;&#x6570;&#xFF0C;&#x7B49;&#x4E8E;&#x8BBE;&#x7F6E;&#x51FD;&#x6570;&#x4F53;&#x5185;this&#x7684;&#x503C;&#x3002;  </span></span><br><span class="line"><span class="built_in">window</span>.color = <span class="string">&apos;red&apos;</span>;</span><br><span class="line"><span class="keyword">var</span> o = {<span class="attr">color</span>: <span class="string">&apos;blue&apos;</span>};</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sayColor</span>(<span class="params"></span>) </span>{</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&quot;color&quot;</span>,<span class="keyword">this</span>.color);</span><br><span class="line">}</span><br><span class="line"><span class="comment">// sayColor.call(o); //&#x76F4;&#x63A5;&#x6253;&#x5370;&#x51FA;&#x6765;&#x4E86;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//&#x800C;bind&#x9700;&#x8981;&#x518D;&#x8C03;&#x7528;&#x4E00;&#x6B21;</span></span><br><span class="line"><span class="keyword">var</span> obj = sayColor.bind(o);</span><br><span class="line">obj();</span><br></pre></td></tr></table></figure>
<p>bind()&#x5C06;&#x51FD;&#x6570;&#x7ED1;&#x5B9A;&#x5230;&#x6307;&#x5B9A;&#x73AF;&#x5883;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x5E26;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x548C;&#x4E00;&#x4E2A;&#x73AF;&#x5883;<br>&#x5B9E;&#x73B0;<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">bind</span>(<span class="params">fn, context</span>) </span>{</span><br><span class="line">  <span class="keyword">return</span> <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">    <span class="keyword">return</span> fn.apply(context, <span class="built_in">arguments</span>);</span><br><span class="line">  }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> handler = {</span><br><span class="line">  mes:<span class="string">&quot;Event handler&quot;</span>,</span><br><span class="line"></span><br><span class="line">  handleClick:<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>{</span><br><span class="line">    alert(<span class="keyword">this</span>.mes);</span><br><span class="line">  }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// type.addEventListener(&apos;click&apos;,bind(handler.handleClick, handler),false);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// es6&#x4E2D;&#x81EA;&#x5E26;&#x7684;bind&#x51FD;&#x6570;</span></span><br><span class="line"><span class="comment">// type.addEventListener(&apos;click&apos;,handler.handleClick.bind(handler),false);</span></span><br></pre></td></tr></table></figure></p>

      
    </div>
    
  </div>
  
    
    <div class="copyright">
        <p><span>本文标题:</span><a href="/2016/09/16/作用域链、闭包及其它/">作用域链、闭包及其它</a></p>
        <p><span>文章作者:</span><a href="/" title="访问 donyyang 的个人博客">donyyang</a></p>
        <p><span>发布时间:</span>2016年09月16日 - 00时40分</p>
        <p><span>最后更新:</span>2016年09月16日 - 02时16分</p>
        <p>
            <span>原始链接:</span><a class="post-url" href="/2016/09/16/作用域链、闭包及其它/" title="作用域链、闭包及其它">http://yoursite.com/2016/09/16/作用域链、闭包及其它/</a>
            <span class="copy-path" data-clipboard-text="原文: http://yoursite.com/2016/09/16/作用域链、闭包及其它/　　作者: donyyang" title="点击复制文章链接"><i class="fa fa-clipboard"></i></span>
            <script src="/js/clipboard.min.js"></script>
            <script> var clipboard = new Clipboard('.copy-path'); </script>
        </p>
        <p>
            <span>许可协议:</span><i class="fa fa-creative-commons"></i> <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/cn/" title="中国大陆 (CC BY-NC-SA 3.0 CN)" target = "_blank">"署名-非商用-相同方式共享 3.0"</a> 转载请保留原文链接及作者。
        </p>
    </div>



    <nav id="article-nav">
        
            <div id="article-nav-newer" class="article-nav-title">
                <a href="/2016/10/12/微信小程序/">
                    微信小程序
                </a>
            </div>
        
        
            <div id="article-nav-older" class="article-nav-title">
                <a href="/2016/08/24/webpack的基本用法/">
                    webpack基础
                </a>
            </div>
        
    </nav>

  
</article>

    <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-作用域链："><span class="toc-number">1.</span> <span class="toc-text">1:作用域链：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-闭包"><span class="toc-number">2.</span> <span class="toc-text">2:闭包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-this及call-apply-bind"><span class="toc-number">3.</span> <span class="toc-text">3:this及call,apply,bind</span></a></li></ol>
</div>
<style>
    .left-col .switch-btn {
        display: none;
    }
    .left-col .switch-area {
        display: none;
    }
</style>

<input type="button" id="tocButton" value="隐藏目录"  title="点击按钮隐藏或者显示文章目录">
<script>
    var valueHide = "隐藏目录";
    var valueShow = "显示目录";

    if ($(".left-col").is(":hidden")) {
        $("#tocButton").attr("value", valueShow);
    }

    $("#tocButton").click(function() {
        if ($("#toc").is(":hidden")) {
            $("#tocButton").attr("value", valueHide);
            $("#toc").slideDown(320);
            $(".switch-btn, .switch-area").fadeOut(300);
        }
        else {
            $("#tocButton").attr("value", valueShow);
            $("#toc").slideUp(350);
            $(".switch-btn, .switch-area").fadeIn(500);
        }
    })

    if ($(".toc").length < 1) {
        $("#toc, #tocButton").hide();
        $(".switch-btn, .switch-area").show();
    }
</script>




    <div class="share">
    <div class="bdsharebuttonbox">
    <a href="#" class="bds_more" data-cmd="more"></a>
    <a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博"></a>
    <a href="#" class="bds_sqq" data-cmd="sqq" title="分享给 QQ 好友"></a>
    <a href="#" class="bds_copy" data-cmd="copy" title="复制网址"></a>
    <a href="#" class="bds_mail" data-cmd="mail" title="通过邮件分享"></a>
    <a href="#" class="bds_weixin" data-cmd="weixin" title="生成文章二维码"></a>
    </div>
    <script>
        window._bd_share_config={
            "common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"24"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
    </script>
</div>



    
      <div class="duoshuo" id="comments">
    <!-- 多说评论框 start -->
    <div class="ds-thread" data-thread-key="2016/09/16/作用域链、闭包及其它/" data-title="作用域链、闭包及其它" data-url="http://yoursite.com/2016/09/16/作用域链、闭包及其它/"></div>
    <!-- 多说评论框 end -->
    <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
    <script type="text/javascript">
    var duoshuoQuery = {short_name:"dony"};
    (function() {
        var ds = document.createElement('script');
        ds.type = 'text/javascript';ds.async = true;
        ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
        ds.charset = 'UTF-8';
        (document.getElementsByTagName('head')[0] 
         || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
    </script>
    <!-- 多说公共JS代码 end -->
</div>

    




    <div class="scroll" id="post-nav-button">
        
            <a href="/2016/10/12/微信小程序/" title="上一篇: 微信小程序">
                <i class="fa fa-angle-left"></i>
            </a>
        

        <a title="文章列表"><i class="fa fa-bars"></i><i class="fa fa-times"></i></a>

        
            <a href="/2016/08/24/webpack的基本用法/" title="下一篇: webpack基础">
                <i class="fa fa-angle-right"></i>
            </a>
        
    </div>

    <ul class="post-list"><li class="post-list-item"><a class="post-list-link" href="/2016/10/12/微信小程序/">微信小程序</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/09/16/作用域链、闭包及其它/">作用域链、闭包及其它</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/08/24/webpack的基本用法/">webpack基础</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/08/13/上传到github上/">将本的的文件上传到github上</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/03/17/hello-world/">Hello World</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/03/17/my-first-blog/">my first blog</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/03/17/js-3种排序算法/">js-3种排序算法</a></li><li class="post-list-item"><a class="post-list-link" href="/2016/03/17/em的用法/">em的用法</a></li></ul>




    <script>
        
    </script>
</div>
      <footer id="footer">
    <div class="outer">
        <div id="footer-info">
            <div class="footer-left">
                &copy; 2018 donyyang
            </div>
            <div class="footer-right">
                <a href="http://hexo.io/" target="_blank" title="快速、简洁且高效的静态博客框架">Hexo</a>  Theme <a href="https://github.com/MOxFIVE/hexo-theme-yelee" target="_blank" title="简而不减双栏 Hexo 博客主题">Yelee</a> by MOxFIVE
            </div>
        </div>
        
            <div class="visit">
                
                    <span id="busuanzi_container_site_pv" style='display:none'>
                        <span id="site-visit" >本站到访数: 
                            <span id="busuanzi_value_site_uv"></span>
                        </span>
                    </span>
                
                
                    <span>, </span>
                
                
                    <span id="busuanzi_container_page_pv" style='display:none'>
                        <span id="page-visit">本页阅读量: 
                            <span id="busuanzi_value_page_pv"></span>
                        </span>
                    </span>
                
            </div>
        
    </div>
</footer>
    </div>
    
<script data-main="/js/main.js" src="//cdn.bootcss.com/require.js/2.1.22/require.min.js"></script>

    <script>
        $(document).ready(function() {
            var iPad = window.navigator.userAgent.indexOf('iPad');
            if (iPad > -1 || $(".left-col").css("display") === "none") {
                var bgColorList = ["#9db3f4", "#414141", "#e5a859", "#f5dfc6", "#c084a0", "#847e72", "#cd8390", "#996731"];
                var bgColor = Math.ceil(Math.random() * (bgColorList.length - 1));
                $("body").css({"background-color": bgColorList[bgColor], "background-size": "cover"});
            }
            else {
                var backgroundnum = 5;
                var backgroundimg = "url(/background/bg-x.jpg)".replace(/x/gi, Math.ceil(Math.random() * backgroundnum));
                $("body").css({"background": backgroundimg, "background-attachment": "fixed", "background-size": "cover"});
            }
        })
    </script>




<div class="scroll" id="scroll">
    <a href="#" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    <a href="#comments" title="查看评论"><i class="fa fa-comments-o"></i></a>
    <a href="#footer" title="转到底部"><i class="fa fa-arrow-down"></i></a>
</div>

    <script src="//cdn.bootcss.com/jqueryui/1.10.4/jquery-ui.min.js"></script>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js">
</script>
  </div>
</body>
</html>